﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using BaseData.Exts;
using BaseLib.Helper;

namespace BaseData.Lib
{
    public interface IRepository<TEntity> where TEntity : class
    {
        #region - DBContext -

        ContextExt DBContext { get; }

        #endregion

        #region - IsExist -

        bool IsExist(Expression<Func<TEntity, bool>> predicate);

        #endregion

        #region - 获取所有的记录 -

        /// <summary>
        /// 获取所有的记录
        /// </summary>
        /// <returns></returns>
        IQueryable<TEntity> All();

        #endregion

        #region - 获取所有的记录 -

        /// <summary>
        /// 获取所有的记录
        /// </summary>
        /// <param name="pageindex">要访问的页</param>
        /// <param name="pagesize">页容量</param>
        /// <returns></returns>
        PagerList<TEntity> All(int pageindex, int pagesize, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy);

        #endregion

        #region - 获取分页数据 -

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">要访问的页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="where">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="includeProperties">贪婪加载的成员</param>
        /// <returns></returns>
        IQueryable<TEntity> GetList(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy, int pageIndex, int pageSize, Expression<Func<TEntity, bool>> where = null, string includeProperties = "");

        #endregion

        #region - 获取分页数据 -

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">要访问的页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="where">条件</param>
        /// <param name="orderBy">排序</param>
        /// <returns>PagedList</returns>
        PagerList<TEntity> GetList(int pageIndex, int pageSize, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy, Expression<Func<TEntity, bool>> where = null);

        #endregion

        #region - 获取列表数据 -

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> where, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null);

        #endregion

        #region - 获取指定ID的记录 -

        /// <summary>
        /// 获取指定ID的记录
        /// </summary>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        TEntity Find(params object[] keyValues);

        #endregion

        #region - 获取单个记录 -

        /// <summary>
        /// 获取单个记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns></returns>
        TEntity Find(Expression<Func<TEntity, bool>> where);

        #endregion

        #region - 插入 -

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">TEntity entity</param>
        TEntity Insert(TEntity entity);

        #endregion

        #region - 插入并保存 -

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">TEntity entity</param>
        TEntity InsertBySave(TEntity entity);

        #endregion

        #region - 删除 -

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValues"></param>
        TEntity Delete(params object[] keyValues);

        #endregion

        #region - 删除并保存 -

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValues"></param>
        TEntity DeleteBySave(params object[] keyValues);

        #endregion

        #region - 删除 -

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">TEntity entity</param>
        TEntity Delete(TEntity entity);

        void Delete(IEnumerable<TEntity> entity);

        #endregion

        #region - 删除并保存 -

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">TEntity entity</param>
        TEntity DeleteBySave(TEntity entity);

        #endregion

        #region - 更新数据库 -

        /// <summary>
        /// 更新数据库
        /// </summary>
        int Save();

        #endregion
    }
}
